package com.db.client.UIs;

import com.db.client.UI;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

//sreenoutput still interferes with input
public class Console implements UI {

    private static InputStreamReader inReader = new InputStreamReader(System.in);
    private static BufferedReader consoleInput = new BufferedReader(inReader);

    private Console() {}

    @Override
    public synchronized void printMessages(String... messages) {
        for (String message: messages) {
            System.err.println("\r" + message.trim());
        }
    }

    @Override
    public String getLine(String string) {
        try {
            System.err.print("\r" + string);
            return consoleInput.readLine();
        } catch (IOException e) {
            return null;
        }
    }

    @Override
    public String getLine() {
        return getLine("");
    }

    @Override
    public void error(String err) {
        System.err.println("\rclient internal : " + err);
    }

    public static UI getConsole() {
        return new Console();
    }
}
